package org.tigris.atlas.examples.persist;

import org.tigris.atlas.examples.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.examples.transfer.SimpleDomain;
import org.tigris.atlas.examples.transfer.SimpleDomainPK;
import org.tigris.atlas.examples.transfer.TransferObjectFactory;

import java.util.Date;

import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

import junit.framework.TestCase;

public class TestSimpleDomainDao extends TestCase {

	private SimpleDomainPK pk;
	EntityMaintenanceServiceDelegate delegate;

	public void setUp() throws Exception {
		super.setUp();
		
		pk = TransferObjectFactory.createSimpleDomainPK();
		delegate = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
		
		
	}

	public void tearDown() throws Exception {
		super.tearDown();
		delegate = null;
	}
	
	public void testInsert() throws Exception {
		Date date = new Date();
		Long aLong = new Long(100);
		
		SimpleDomain simpleDomain = TransferObjectFactory.createSimpleDomain();
		simpleDomain.setName("Name");
		simpleDomain.setType("A");
		simpleDomain.setTheDate1(date);
		simpleDomain.setTheLong1(aLong);
		simpleDomain = delegate.save(simpleDomain).getSimpleDomain();
		assertNotNull(simpleDomain.getId());
		
		pk.setId(simpleDomain.getId());
		SimpleDomain to = delegate.findByPrimaryKey(pk).getSimpleDomain();
		
		assertEquals("Name", to.getName());
		assertEquals("A", to.getType());
		//assertEquals(date, simpleDomain.getTheDate1());
		assertEquals(aLong, to.getTheLong1());
	}
	
	public void testFindByPrimaryKey() {
		SimpleDomain simpleDomain = TransferObjectFactory.createSimpleDomain();
		simpleDomain.setName("Name");
		simpleDomain.setType("A");
		simpleDomain.setTheDate1(new Date());
		simpleDomain.setTheLong1(new Long(1000));
		
		simpleDomain = delegate.save(simpleDomain).getSimpleDomain();
		String id = simpleDomain.getId();
		assertNotNull(id);
		
		pk.setId(id);
		SimpleDomain to = delegate.findByPrimaryKey(pk).getSimpleDomain();
		assertEquals(id, to.getId());
	}
	
	public void testUpdate() {
		long millis = System.currentTimeMillis();
		Date date = new Date(millis);
		Long aLong = new Long(millis);
		
		SimpleDomain simpleDomain = TransferObjectFactory.createSimpleDomain();
		simpleDomain.setName("Name");
		simpleDomain.setType("A");
		simpleDomain.setTheDate1(date);
		simpleDomain.setTheLong1(aLong);
		
		simpleDomain = delegate.save(simpleDomain).getSimpleDomain();
		String id = simpleDomain.getId();
		assertNotNull(id);
		
		pk.setId(id);
		assertEquals("Name", simpleDomain.getName());
		assertEquals("A", simpleDomain.getType());
		//assertEquals(date, simpleDomain.getTheDate1());
		assertEquals(aLong, simpleDomain.getTheLong1());
		
		Date newDate = new Date(millis + 10000);
		Long newLong = new Long(millis + 10000);
		
		simpleDomain.setName("NewName");
		simpleDomain.setType("B");
		simpleDomain.setTheDate1(newDate);
		simpleDomain.setTheLong1(newLong);
		
		simpleDomain = delegate.save(simpleDomain).getSimpleDomain();
		assertEquals(id, simpleDomain.getId());
		assertEquals("NewName", simpleDomain.getName());
		assertEquals("B", simpleDomain.getType());
		//assertEquals(newDate, simpleDomain.getTheDate1());
		assertEquals(newLong, simpleDomain.getTheLong1());
		
		simpleDomain = delegate.findByPrimaryKey(pk).getSimpleDomain();
		assertEquals(id, simpleDomain.getId());
		assertEquals("NewName", simpleDomain.getName());
		assertEquals("B", simpleDomain.getType());
		//assertEquals(newDate, simpleDomain.getTheDate1());
		assertEquals(newLong, simpleDomain.getTheLong1());
	}
	
	public void testDelete() {
		SimpleDomain simpleDomain = TransferObjectFactory.createSimpleDomain();
		simpleDomain.setName("Name");
		simpleDomain.setType("A");
		simpleDomain.setTheDate1(new Date());
		simpleDomain.setTheLong1(new Long(1000));
		
		simpleDomain = delegate.save(simpleDomain).getSimpleDomain();
		String id = simpleDomain.getId();
		assertNotNull(id);
		
		pk.setId(id);
		assertNotNull(delegate.findByPrimaryKey(pk).getSimpleDomain());
		
		delegate.delete(simpleDomain.getSimpleDomainPK());
		assertEquals(id, simpleDomain.getId());
		
		SimpleDomain to = delegate.findByPrimaryKey(pk).getSimpleDomain();
		assertNull(to);
	}
	
}
